import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedList;

/**
 * This class used to store the group of points that we need to apply with map
 * matching.
 * 
 * Processing : After query points from a table -> send the property of GPSpnt
 * (row_id,source_id,lat,long,speed,heading,time_stamp) and then keep the object
 * in batch.
 * */
public class SourceGPS {

	/**
	 * if null, no data has been processed
	 */

	Connection conn_mainDB;
	Statement s;
	ResultSet r;

	public SourceGPS(Connection conn_mainDB) {
		this.conn_mainDB = conn_mainDB;

	}

	public LinkedList<GPSpnt> getBatch() {
		LinkedList<GPSpnt> batch = new LinkedList<GPSpnt>();

		// get the right batch, considering constraints
		try {
			s = conn_mainDB.createStatement();

			String sql = String
					.format("SELECT row_id,source_id,lat,long,speed,heading,time_stamp FROM \"%s\".\"%s\" order by row_id",
							Config.SCHEMA_NAME_GPS_DATA,
							Config.TABLE_NAME_GPS_DATA);

			System.out.println("(SourceGPS)- " + sql);

			r = s.executeQuery(sql);

			while (r.next()) {
				GPSpnt p = new GPSpnt(this, r.getInt("row_id"),
						r.getString("source_id"), r.getDouble("lat"),
						r.getDouble("long"), r.getDouble("speed"),
						r.getDouble("heading"), r.getString("time_stamp"));
				batch.add(p);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		}
		// System.out.println(batch.size());
		// FIXME complete
		return batch;
	}

	public LinkedList<GPSpnt> getBatchWithLink() {
		LinkedList<GPSpnt> batch = new LinkedList<GPSpnt>();

		// get the right batch, considering constraints
		try {
			s = conn_mainDB.createStatement();

			String sql = String
					.format("SELECT row_id,source_id, \"linkID\", lamda, time_stamp,\"dayName\" FROM \"%s\".\"%s\" WHERE \"linkID\" is not NULL AND \"isTarget\" = TRUE order by row_id",
							Config.SCHEMA_NAME_GPS_DATA,
							Config.TABLE_NAME_GPS_DATA);
			
			r = s.executeQuery(sql);

			while (r.next()) {
				GPSpnt p = new GPSpnt(this, r.getInt("row_id"),r.getString("source_id"),
						r.getInt("linkID"), r.getDouble("lamda"),
						r.getString("time_stamp"),r.getString("dayName"));
				batch.add(p);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		}
		// System.out.println(batch.size());
		// FIXME complete
		return batch;
	}

}
